package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.DzWendaback;
import com.ruoyi.system.mapper.DzWendaMapper;
import com.ruoyi.system.domain.DzWenda;
import com.ruoyi.system.service.IDzWendaService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 问答信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-07
 */
@Service
public class DzWendaServiceImpl implements IDzWendaService 
{
    @Autowired
    private DzWendaMapper dzWendaMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询问答信息
     * 
     * @param wdId 问答信息主键
     * @return 问答信息
     */
    @Override
    public DzWenda selectDzWendaByWdId(Long wdId)
    {
        return dzWendaMapper.selectDzWendaByWdId(wdId);
    }

    /**
     * 查询问答信息列表
     * 
     * @param dzWenda 问答信息
     * @return 问答信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DzWenda> selectDzWendaList(DzWenda dzWenda)
    {

        //权限控制
        return dzWendaMapper.selectDzWendaList(dzWenda);
    }


    @Override
    public List<DzWenda> selectDzWendaList2(DzWenda dzWenda)
    {
        //权限控制
        return dzWendaMapper.selectDzWendaList2(dzWenda);
    }

    @Override
    public List<DzWenda> selectDzWendaListWeb(DzWenda dzWenda)
    {
        return dzWendaMapper.selectDzWendaList(dzWenda);
    }

    /**
     * 新增问答信息
     * 
     * @param dzWenda 问答信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDzWenda(DzWenda dzWenda)
    {
        dzWenda.setCreateTime(DateUtils.getNowDate());
        int rows = dzWendaMapper.insertDzWenda(dzWenda);
        insertDzWendaback(dzWenda);
        return rows;
    }

    /**
     * 修改问答信息
     * 
     * @param dzWenda 问答信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDzWenda(DzWenda dzWenda)
    {
        dzWendaMapper.deleteDzWendabackByWdId(dzWenda.getWdId());
        insertDzWendaback(dzWenda);
        return dzWendaMapper.updateDzWenda(dzWenda);
    }

    /**
     * 批量删除问答信息
     * 
     * @param wdIds 需要删除的问答信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzWendaByWdIds(Long[] wdIds)
    {
        dzWendaMapper.deleteDzWendabackByWdIds(wdIds);
        return dzWendaMapper.deleteDzWendaByWdIds(wdIds);
    }

    /**
     * 删除问答信息信息
     * 
     * @param wdId 问答信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzWendaByWdId(Long wdId)
    {
        dzWendaMapper.deleteDzWendabackByWdId(wdId);
        return dzWendaMapper.deleteDzWendaByWdId(wdId);
    }

    /**
     * 新增回答信息
     * 
     * @param dzWenda 问答信息对象
     */
    public void insertDzWendaback(DzWenda dzWenda)
    {
        List<DzWendaback> dzWendabackList = dzWenda.getDzWendabackList();
        Long wdId = dzWenda.getWdId();
        if (StringUtils.isNotNull(dzWendabackList))
        {
            List<DzWendaback> list = new ArrayList<DzWendaback>();
            for (DzWendaback dzWendaback : dzWendabackList)
            {
                dzWendaback.setWdId(wdId);
                list.add(dzWendaback);
            }
            if (list.size() > 0)
            {
                dzWendaMapper.batchDzWendaback(list);
            }
        }
    }
}
